package com.yowasa.xiuxian.engine.message.hone;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Sets;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.CommonEngine;
import com.yowasa.xiuxian.engine.HoneEngine;
import com.yowasa.xiuxian.engine.hone.MentalInterface;
import com.yowasa.xiuxian.engine.hone.SpecialInterface;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.MessageFormat;
import java.util.Set;
import java.util.stream.Collectors;

@CMD(name = "/学习神通", scope = CmdScopeEnum.ALL)
public class StudySpecialHandler extends CmdMessageHandler {
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private MessageHelper messageHelper;

    @Override
    public void execute(SendMessage message) {
        UserBO user = userHelper.checkOpUserAll(message);
        String select = getParam(message);
        Set<String> allStrategy = CommonEngine.SPECIAL_EFFECT.keySet();
        if (user.getUserConfig().getStudiedSpecialEffect().contains(select)) {
            throw new HandlerException("你已经学习过此神通，无需再次学习");
        }
        Set<String> allSpecialEffect = Sets.newConcurrentHashSet();
        Set<String> knownSpecialEffect = user.getUserConfig().getKnownSpecialEffect();
        allSpecialEffect.addAll(knownSpecialEffect);
        String mentalStr = user.getUserConfig().getMental();
        if (StringUtils.isNotBlank(mentalStr)) {
            MentalInterface mental = HoneEngine.mentalMap.get(mentalStr);
            allSpecialEffect.addAll(mental.specialList());
        }
        allSpecialEffect.removeAll(user.getUserConfig().getStudiedSpecialEffect());

        if (StringUtils.isBlank(select)) {
            messageHelper.replyMys(message, "可学习的神通有:\n" + allSpecialEffect.stream().collect(Collectors.joining("\n")));
            return;
        }
        if (!allStrategy.contains(select)) {
            throw new HandlerException("未识别神通");
        }
        if (!allSpecialEffect.contains(select)) {
            throw new HandlerException("你未获得过该神通的修习之法");
        }
        SpecialInterface specialInterface = CommonEngine.SPECIAL_EFFECT.get(select);
        if (user.getUserConfig().getStudiedSpecialEffect().contains("太玄妙法") && user.getUserAttr().getUnd() <= 30) {
        } else {
            specialInterface.limit(user);
        }
        int need = specialInterface.calNeedTravel(user);
        int upAbility = specialInterface.userUpAbility(user);
        int travel = user.getUserConfig().getTravel();
        if (travel < need) {
            throw new HandlerException(MessageFormat.format("你的历练值不够，无法学习[{0}/{1}]", travel, need));
        }
        user.getUserConfig().setTravel(travel - need);
        user.getUserConfig().setAbility(user.getUserConfig().getAbility() + upAbility);
        user.getUserConfig().getStudiedSpecialEffect().add(select);
        specialInterface.effectOnce(user);
        userHelper.rebuildBattleInfo(user);
        userHelper.saveUserInfo(user);
        messageHelper.replyMys(message, MessageFormat.format("你消耗了{0}点历练值习得了{1}", need, select));
    }

    public String desc(){
        return new StringBuffer().append("神通一般为被动或者功能性的效果\n")
                .append("使用方法 /学习神通 神通名称 ，不带名称会展示所有当前可学习的神通")
                .toString();

    }
}
